package com.android.qjpmc.data_module;

import com.android.qjpmc.data_module.PwrStation;

public class PwrStationSet {
    PwrStation m_Head = null;

    public PwrStation GetHead()
    {
        return m_Head;
    }

    void SetHead(PwrStation Device)
    {
        m_Head = Device;
    }
    /*
     * 获取尾部节点
     *
     *
     * */
    PwrStation GetTail()
    {
        PwrStation Head = GetHead();
        PwrStation Next = null;
        if (Head == null)
        {
            return null;
        }
        Next = Head.GetNext();
        while (Next != null)
        {
            Head = Next;
            Next = Head.GetNext();
        }
        return Head;
    }
    /*
     * 把站节点加入到链表尾部
     * Station：站节点对象
     * */
    public int AppendAStation(PwrStation Station)
    {
        PwrStation Tail = GetTail();
        if (Tail == null)
        {
            SetHead(Station);
        }
        else
        {
            Tail.AppendANode(Station);
        }
        return 0;
    }
    /*
     * 获取站节点：
     * ID:站节点ID
     *
     *
     * */
    public PwrStation GetPwrStation(int ID)
    {
        PwrStation Station = GetHead();
        while(Station != null)
        {
            if (Station.IsMe(ID) == 1)
            {
                return  Station;
            }
            Station = Station.GetNext();
        }
        return Station;
    }
    //移除站节点
    public int RemoveStation(int StationID)
    {
        PwrStation Head = GetHead();
        PwrStation PreNode = null;
        PwrStation NextNode = null;
        PwrStation TheNode = null;
        TheNode = Head;
        while(TheNode != null)
        {
            if (TheNode.IsMe(StationID) == 1)
            {
                PreNode = TheNode.GetNext();
                NextNode = TheNode.GetPre();
                if (TheNode == Head)
                {
                    SetHead(NextNode);
                    TheNode = null;
                }
                else
                {
                    if (PreNode != null)
                    {
                        PreNode.SetNext(NextNode);
                    }
                    if (NextNode != null)
                    {
                        NextNode.SetPre(PreNode);
                    }
                }
                return 0;

            }
            TheNode = TheNode.GetNext();
        }
        return 1;
    }

    public int Release()
    {
        SetHead(null);
        return 0;
    }
}
